package com.sentiment.strategy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.sentiment.config.Config;

public class TitleFilter {
	public static void loadConfiguration() {
		Logger logger = Config.getLogger(new TitleFilter().getClass());
		SAXReader saxReader = new SAXReader();
		Document doc;
		Element root;

		try {
			doc = saxReader.read(new FileInputStream(new File(Config.xmlTitleFilter)));
			root = doc.getRootElement();
			int illegalAmount = Integer.parseInt(root.element("illegal").elementText("amount"));
			for (int i = 1; i <= illegalAmount; i++) {
//				logger.info(root.element("illegal").elementText("No"+i));
				Config.illegal.add(root.element("illegal").elementText("No" + i));
			}
			int permissionAmount = Integer.parseInt(root.element("permission").elementText("amount"));
			for (int i = 1; i <= permissionAmount; i++) {
//				logger.info(root.element("permission").elementText("No"+i));
				Config.permission.add(root.element("permission").elementText("No" + i));
			}
		} catch (FileNotFoundException | DocumentException e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		logger.info("-----读取***标题过滤***配置文件完成-----");
//		logger.info("-----打印标题配置文件内容-----");
		logger.info(Config.illegal.toString());
		logger.info(Config.permission.toString());
	}

	/**
	 * 过滤标题 如果包含在illegal则返回true 表示需要被过滤 否则返回false
	 * 
	 * @return
	 */
	public static boolean filter(String title) {

		Iterator<String> iter = Config.illegal.iterator();
		while (iter.hasNext()) {
			if (title.contains(iter.next())) {
				return true;
			}
		}
		return false;
	}
}
